﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectSchedule.Common;

namespace ProjectSchedule.Entity
{
    public class CTSectionProgress
    {
        // 工程号
        public string ShipName { get; set; }
        // 分段名称
        public string SectionName { get; set; }
        // 重量
        public double Weight { get; set; }
        // 开工日
        public  DateTime BeginDate { get; set; }
        public  double BeginPrecent { get; set; }
        // 完工日
        public  DateTime EndDate { get; set; }
        public  double EndPrecent { get; set; }

        public DateTime PlanStartDate { get; set; }
        public DateTime PlanEndDate { get; set; }
        public Int32 PlanDays
        {
            get
            {
                return (PlanEndDate - PlanStartDate).Days + 1;
            }
        }

        public string UnitName { get; set; }

        public bool IsBegin { get{
            return BeginDate.Equals(PlanStartDate);
        } }
        public bool IsComplete { get {
            return EndDate.Equals(PlanEndDate);
        } }

        public static CTSectionProgress GetProgerssByPlan(CTSectionPlan Plan, CTProgressType Type, DateTime beginDate, DateTime endDate)
        {
            var model = new CTSectionProgress();
            model.ShipName = Plan.ShipName;
            model.SectionName = Plan.SectionName;
            model.Weight = Plan.Weight;
            switch (Type)
            {
                case CTProgressType.Pretreat:
                    if (Plan.PretreatStop < beginDate ||
                        Plan.PretreatStart > endDate||
                        Plan.PlanSectionTime.Pretreat<=0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.PretreatStart.Value;
                    model.PlanEndDate = Plan.PretreatStop.Value;
                    model.BeginDate = Plan.PretreatStart.Value <= beginDate ? beginDate : Plan.PretreatStart.Value;
                    model.EndDate = Plan.PretreatStop.Value >= endDate ? endDate : Plan.PretreatStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.PretreatStart.Value).Days * 100 / Plan.PlanSectionTime.Pretreat;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.PretreatStart.Value).Days * 100 / Plan.PlanSectionTime.Pretreat;
                    //model.UnitName = OpExcel.GetProcessName("预处理单位", Plan.Units.DealUnit);
                    model.UnitName = Plan.Units.DealUnit;
                    break;
                case CTProgressType.Cutting:
                    if (Plan.CuttingStop < beginDate ||
                        Plan.CuttingStart > endDate ||
                        Plan.PlanSectionTime.Cutting <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.CuttingStart.Value;
                    model.PlanEndDate = Plan.CuttingStop.Value;
                    model.BeginDate = Plan.CuttingStart.Value <= beginDate ? beginDate : Plan.CuttingStart.Value;
                    model.EndDate = Plan.CuttingStop.Value >= endDate ? endDate : Plan.CuttingStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.CuttingStart.Value).Days * 100 / Plan.PlanSectionTime.Cutting;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.CuttingStart.Value).Days * 100 / Plan.PlanSectionTime.Cutting;
                    //model.UnitName = OpExcel.GetProcessName("下料单位", Plan.Units.CuttUnit);
                    model.UnitName = Plan.Units.CuttUnit;
                    break;
                case CTProgressType.SmallGroup:
                    if (Plan.SmallGroupStop < beginDate ||
                        Plan.SmallGroupStart > endDate ||
                        Plan.PlanSectionTime.SmallGroup <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.SmallGroupStart.Value;
                    model.PlanEndDate = Plan.SmallGroupStop.Value;
                    model.BeginDate = Plan.SmallGroupStart.Value <= beginDate ? beginDate : Plan.SmallGroupStart.Value;
                    model.EndDate = Plan.SmallGroupStop.Value >= endDate ? endDate : Plan.SmallGroupStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.SmallGroupStart.Value).Days * 100 / Plan.PlanSectionTime.SmallGroup;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.SmallGroupStart.Value).Days * 100 / Plan.PlanSectionTime.SmallGroup;
                    //model.UnitName = OpExcel.GetProcessName("小组立单位", Plan.Units.SmallGroupUnit);
                    model.UnitName = Plan.Units.SmallGroupUnit;
                    break;
                case CTProgressType.MediumGroup:
                    if (Plan.MediumGroupStop < beginDate ||
                        Plan.MediumGroupStart > endDate ||
                        Plan.PlanSectionTime.MediumGroup <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.MediumGroupStart.Value;
                    model.PlanEndDate = Plan.MediumGroupStop.Value;
                    model.BeginDate = Plan.MediumGroupStart.Value <= beginDate ? beginDate : Plan.MediumGroupStart.Value;
                    model.EndDate = Plan.MediumGroupStop.Value >= endDate ? endDate : Plan.MediumGroupStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.MediumGroupStart.Value).Days * 100 / Plan.PlanSectionTime.MediumGroup;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.MediumGroupStart.Value).Days * 100 / Plan.PlanSectionTime.MediumGroup;
                    //model.UnitName = OpExcel.GetProcessName("中组立单位", Plan.Units.MedGroupUnit);
                    model.UnitName = Plan.Units.MedGroupUnit;
                    break;
                case CTProgressType.MediumMergeA:
                    if (Plan.MediumMergeAStop < beginDate || 
                        Plan.MediumMergeAStart > endDate||
                        Plan.PlanSectionTime.MediumMergeA <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.MediumMergeAStart.Value;
                    model.PlanEndDate = Plan.MediumMergeAStop.Value;
                    model.BeginDate = Plan.MediumMergeAStart.Value <= beginDate ? beginDate : Plan.MediumMergeAStart.Value;
                    model.EndDate = Plan.MediumMergeAStop.Value >= endDate ? endDate : Plan.MediumMergeAStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.MediumMergeAStart.Value).Days * 100 / Plan.PlanSectionTime.MediumMergeA;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.MediumMergeAStart.Value).Days * 100 / Plan.PlanSectionTime.MediumMergeA;
                    //model.UnitName = OpExcel.GetProcessName("中合单位", Plan.Units.MedUnit);
                    model.UnitName = Plan.Units.MedAUnit;
                    model.SectionName = Plan.SectionName;
                    break;
                case CTProgressType.MediumPainting:
                    if (Plan.MediumPaintingStop < beginDate || 
                        Plan.MediumPaintingStart > endDate || 
                        Plan.PlanSectionTime.MediumPainting <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.MediumPaintingStart.Value;
                    model.PlanEndDate = Plan.MediumPaintingStop.Value;
                    model.BeginDate = Plan.MediumPaintingStart.Value <= beginDate ? beginDate : Plan.MediumPaintingStart.Value;
                    model.EndDate = Plan.MediumPaintingStop.Value >= endDate ? endDate : Plan.MediumPaintingStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.MediumPaintingStart.Value).Days * 100 / Plan.PlanSectionTime.MediumPainting;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.MediumPaintingStart.Value).Days * 100 / Plan.PlanSectionTime.MediumPainting;
                    //model.UnitName = OpExcel.GetProcessName("中涂单位", Plan.Units.MidPaintUnit);
                    model.UnitName = Plan.Units.MidPaintUnit;
                    model.SectionName = Plan.SectionName;
                    break;
                case CTProgressType.MediumMergeB:
                    if (Plan.MediumMergeBStop < beginDate ||
                        Plan.MediumMergeBStart > endDate ||
                        Plan.PlanSectionTime.MediumMergeB <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.MediumMergeBStart.Value;
                    model.PlanEndDate = Plan.MediumMergeBStop.Value;
                    model.BeginDate = Plan.MediumMergeBStart.Value <= beginDate ? beginDate : Plan.MediumMergeBStart.Value;
                    model.EndDate = Plan.MediumMergeBStop.Value >= endDate ? endDate : Plan.MediumMergeBStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.MediumMergeBStart.Value).Days * 100 / Plan.PlanSectionTime.MediumMergeB;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.MediumMergeBStart.Value).Days * 100 / Plan.PlanSectionTime.MediumMergeB;
                    //model.UnitName = OpExcel.GetProcessName("中合单位", Plan.Units.MedUnit);
                    model.UnitName = Plan.Units.MedBUnit;
                    model.SectionName = Plan.SectionName;
                    break;
                case CTProgressType.LargeMerge:
                    if (Plan.LargeMergeStop < beginDate || 
                        Plan.LargeMergeStart > endDate|| 
                        Plan.PlanSectionTime.LargeMerge <= 0)
                    {
                        return null;
                    }
                    model.PlanStartDate = Plan.LargeMergeStart.Value;
                    model.PlanEndDate = Plan.LargeMergeStop.Value;
                    model.BeginDate = Plan.LargeMergeStart.Value <= beginDate ? beginDate : Plan.LargeMergeStart.Value;
                    model.EndDate = Plan.LargeMergeStop.Value >= endDate ? endDate : Plan.LargeMergeStop.Value;
                    model.BeginPrecent = (model.BeginDate - Plan.LargeMergeStart.Value).Days * 100 / Plan.PlanSectionTime.LargeMerge;
                    model.EndPrecent = (model.EndDate.AddDays(1) - Plan.LargeMergeStart.Value).Days * 100 / Plan.PlanSectionTime.LargeMerge;
                    //model.UnitName = OpExcel.GetProcessName("大合单位", Plan.Units.BigUnit);
                    model.UnitName = Plan.Units.BigUnit;
                    model.SectionName = Plan.SectionName;
                    break;
                default:
                    return null;
            }
            return model;
        }

    }
}
